package com.gxa.autoparts.portal.service.impl;

import com.gxa.autoparts.portal.service.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author ：looker
 * @date ：Created in 2022/1/6 15:44
 */
@Service
public class SearchServiceImpl implements SearchService {

    @Autowired
    RedisTemplate<String,Object> redisTemplate;

    private final static String HISTORYHEAD = "HISTORY:";

    private final static String HOT = "HOT";

//    //导入数据源
//    @Resource(name = "redisSearchTemplate")
//    private StringRedisTemplate redisSearchTemplate;


    //新增一条该userid用户在搜索栏的历史记录
    //searchkey 代表输入的关键词
    public int addSearchHistoryByUserId(Long userid, String searchkey) {
        HashOperations<String, Object, Object> hashOperations = redisTemplate.opsForHash();

        Boolean aBoolean = redisTemplate.hasKey(HISTORYHEAD + userid);
        if(aBoolean){
            //该键存在   再看改键下是否有该关键词存在
            Object o = hashOperations.get(HISTORYHEAD + userid, searchkey);
            if(o!=null){
                return 1;
            }else {
                hashOperations.put(HISTORYHEAD + userid,searchkey,"1");
            }
        }else {
            hashOperations.put(HISTORYHEAD + userid,searchkey,"1");
        }
        return 1;
    }

    //删除个人历史数据
    @Override
    public Boolean delSearchHistoryByUserId() {
        Long userid = 1L;
        Boolean delete = redisTemplate.delete(HISTORYHEAD + userid);
        return delete;
    }

    //获取个人历史数据列表
    @Override
    public Set<Object> getSearchHistoryByUserId() {
        Long userid = 1L;
        HashOperations<String, Object, Object> hashOperations = redisTemplate.opsForHash();
        Set<Object> keys = hashOperations.keys(HISTORYHEAD + userid);
        return keys;
    }


    /**
     *   根据searchkey搜索其相关最热的前7名
     */
    @Override
    public Set<Object> getHotList() {
        List<String> result = new ArrayList<>();
        ZSetOperations<String, Object> operations = redisTemplate.opsForZSet();
        Set<Object> range = operations.range(HOT, -7, -1);
        return range;
    }

    @Override
    public void searchWord(String searchkey) {
        if(searchkey==null || "".equals(searchkey)){
            return;
        }
        boolean flag = false;
        ZSetOperations<String, Object> operations = redisTemplate.opsForZSet();
        Set<Object> objects = operations.rangeByScore(HOT, 0, Double.MAX_VALUE);
        for (Object object : objects) {
            if (object.toString().contains(searchkey)) {
                //如果包含，原热词热度加1
                operations.incrementScore(HOT,object.toString(),1L);
                flag = true;
                break;
            }
        }
        if(!flag){
            operations.add(HOT,searchkey,1L);
        }

        this.addSearchHistoryByUserId(1L,searchkey);
    }
}
